package com.sx.gongzhong.voiceamr;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.CountDownTimer;
import android.util.Log;
import android.widget.TextView;

import com.sx.gongzhong.utils.ToastUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**网络录音播放
 * Created by Administrator on 2017/3/10.
 */

public class Player implements MediaPlayer.OnPreparedListener, MediaPlayer.OnBufferingUpdateListener,
        MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener {

    public MediaPlayer mediaPlayer;
    private Context context;
    public Player(Context context) {
        this.context = context;
        initPlayer();
    }

    private void initPlayer() {
        try {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setLooping(false);
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setOnCompletionListener(this);
            mediaPlayer.setOnBufferingUpdateListener(this);//网络流媒体的缓冲监听
            mediaPlayer.setOnPreparedListener(this);//当MediaPlayer调用prepare()方法时触发该监听器
            mediaPlayer.setOnErrorListener(this);
        } catch (Exception e) {
            Log.e("mediaPlayer", "error", e);
        }
    }
    private OnCompletionListener onCompletionListener;
    public interface OnCompletionListener{
        void onPlayComplet();
    }
    private TextView numTime;
    public void playUrl(String videoUrl, TextView textView, OnCompletionListener onCompletionListener){
        this.onCompletionListener = onCompletionListener;
        this.numTime = textView;
        if (mediaPlayer == null){
            initPlayer();
        }
        try {
            mediaPlayer.reset();//重置MediaPlayer对象
            mediaPlayer.setDataSource(videoUrl);
            mediaPlayer.prepare();//prepare之后自动播放
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            if (onCompletionListener != null){
                onCompletionListener.onPlayComplet();
            }
            ToastUtils.showToast(context,"播放错误");
            File file = new File(videoUrl);
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
                mediaPlayer.setDataSource(fis.getFD());
                mediaPlayer.prepare();
                stop();
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }

        } catch (IllegalStateException e) {
            e.printStackTrace();
            if (onCompletionListener != null){
                onCompletionListener.onPlayComplet();
            }
            ToastUtils.showToast(context,"播放错误");
            File file = new File(videoUrl);
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
                mediaPlayer.setDataSource(fis.getFD());
                mediaPlayer.prepare();
                stop();
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
            ToastUtils.showToast(context,"播放错误");
            if (onCompletionListener != null){
                onCompletionListener.onPlayComplet();
            }
            File file = new File(videoUrl);
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
                mediaPlayer.setDataSource(fis.getFD());
                mediaPlayer.prepare();
                stop();
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    public boolean isPlay(){
        if (mediaPlayer != null){
            return mediaPlayer.isPlaying();
        }
        return false;
    }

    public void start() {
        if (mediaPlayer != null){
            mediaPlayer.start();
        }
        new CountDownTimer(duration, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                //将秒转换成分秒
                Date dates = new Date(millisUntilFinished);
                SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
                String times = sdf.format(dates);
                if (numTime != null){
                    numTime.setText("···"+times+"···");
                }
            }
            @Override
            public void onFinish() {
                if (numTime != null){
                    numTime.setText("···00:00···");
                }
                this.cancel();
            }
        }.start();
    }

    public void stop(){
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }
    int duration = 0 ;
    /**
     * 通过onPrepared播放
     */
    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        duration = mediaPlayer.getDuration();
        start();
    }


    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
        stop();
        if (onCompletionListener != null){
            onCompletionListener.onPlayComplet();
        }
    }

    @Override
    public void onBufferingUpdate(MediaPlayer arg0, int bufferingProgress) {
//        skbProgress.setSecondaryProgress(bufferingProgress);
//        int currentProgress = skbProgress.getMax() * mediaPlayer.getCurrentPosition() / mediaPlayer.getDuration();
//        Log.e(currentProgress+"% play", bufferingProgress + "% buffer");
    }

    @Override
    public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
        ToastUtils.showToast(context,"播放错误");
        stop();
        return false;
    }
}
